<div class="layui-card-header layui-card">
    <span class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a>资源管理</a>
        <a><cite>编辑资源</cite></a>
    </span>
</div>
<style>
    .layui-form-label {
        width:15% !important;
        margin-left:1%;
    }
    .layui-input-inline {
        width: 33% !important;
    }

    .saveBtn {
        margin-left: 60px;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">编辑资源</li>
            </ul>
            <div class="layui-tab-content">
                <form class="layui-form  layui-form-pane" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list">
                    <div class="layui-form-item">
                        <label class="layui-form-label">资源ID</label>
                        <div class="layui-input-inline">
                            <input type="text" id="resourceId" name="resourceId" readonly="readonly" class="layui-input">
                        </div>
                        <label class="layui-form-label">父资源ID</label>
                        <div class="layui-input-inline">
                            <input type="text" id="parentId" name="parentId" readonly="readonly" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">资源名称</label>
                        <div class="layui-input-inline">
                            <input type="text" id="name" name="name" lay-verify="required" placeholder="输入资源名称" autocomplete="off" class="layui-input">
                        </div>
                        <label class="layui-form-label">资源标题</label>
                        <div class="layui-input-inline">
                            <input type="text" id="title" name="title" lay-verify="required" placeholder="输入资源名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">跳转URL</label>
                        <div class="layui-input-inline">
                            <input type="text" id="jump" name="jump" placeholder="输入跳转URL" autocomplete="off" class="layui-input">
                        </div>
                        <label class="layui-form-label">授权名</label>
                        <div class="layui-input-inline">
                            <input type="text" id="permName" name="permName" placeholder="输入授权名" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">授权URL</label>
                        <div class="layui-input-inline">
                            <input type="text" id="permUrl" name="permUrl" placeholder="输入授权URL" autocomplete="off" class="layui-input">
                        </div>
                        <label class="layui-form-label">资源类型</label>
                        <div class="layui-input-inline">
                            <input type="radio" name="kind" value="1" title="菜单" checked="checked">
                            <input type="radio" name="kind" value="2" title="按钮">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">所属系统</label>
                        <div class="layui-input-inline">
                            <input type="radio" name="systemType" value="1" title="运营平台" checked="checked">
                            <input type="radio" name="systemType" value="2" title="商户系统">
                            <input type="radio" name="systemType" value="3" title="代理商系统">
                        </div>
                        <label class="layui-form-label">菜单图标</label>
                        <div class="layui-input-inline">
                            <input type="text" id="icon" name="icon" placeholder="输入icon图标" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">排序值</label>
                        <div class="layui-input-inline">
                            <input type="text" id="orderNum" name="orderNum" placeholder="输入排序值" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button type="button" class="layui-btn" lay-submit="" lay-filter="resourceEdit">保存</button>
                            <a class="layui-btn backBtn" onclick="back()">返回</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['table','util','admin', 'form'], function() {
        var table = layui.table
            ,$ = layui.$
            ,form = layui.form
            ,admin = layui.admin
            ,element = layui.element;

        element.render('breadcrumb', 'breadcrumb'); // 渲染导航信息

        var resourceId = layui.router().search.resourceId;
        admin.req({
            type: 'get',
            url: layui.setter.baseUrl + '/sys/resource/get',
            data: {
                resourceId: resourceId
            },
            error: function (err) {
                layer.alert(JSON.stringify(err.field), {
                    title: '错误提示'
                })
            },
            success: function (res) {
                if (res.code == 0) {
                    // 赋值
                    data = res.data;

                    form.render(null, 'layuiadmin-app-form-list');
                    $("#resourceId").val(data.resourceId);
                    $("#parentId").val(data.parentId);
                    $("#name").val(data.name);
                    $("#title").val(data.title);
                    $("#jump").val(data.jump);
                    $("#icon").val(data.icon);
                    $("#orderNum").val(data.orderNum);
                    $("#permName").val(data.permName);
                    $("#permUrl").val(data.permUrl);
                    if (res.data.kind == 1) {
                        $("input[name='kind'][value='1']").attr("checked", true);
                    } else if ((res.data.kind == 2)) {
                        $("input[name='kind'][value='2']").attr("checked", true);
                    }
                    if (res.data.systemType == 1) {
                        $("input[name='systemType'][value='1']").attr("checked", true);
                    } else if ((res.data.systemType == 2)) {
                        $("input[name='systemType'][value='2']").attr("checked", true);
                    } else if ((res.data.systemType == 3)) {
                        $("input[name='systemType'][value='3']").attr("checked", true);
                    }
                    form.render();
                }
            }
        })

        form.on('submit(resourceEdit)', function (data) {
            // 这里可以写ajax方法提交表单
            $.ajax({
                type: "POST",
                url: layui.setter.baseUrl + '/sys/resource/update',
                data: "params=" + JSON.stringify(data.field),
                success: function (res) {
                    if (res.code == 0) {
                        /*layer.alert(res.msg,{
                            title: "保存成功"
                        }, function(index) {
                            table.reload('tableReload');
                            layer.closeAll();
                         })*/
                        layer.msg('保存成功', {
                            offset: '15px'
                            , icon: 1
                            , time: 1000
                        }, function () {
                            back();
                        });
                    } else {
                        /*layer.alert(res.msg, {
                            title: '保存失败'
                         });*/
                        layer.msg('保存失败', {
                            offset: '15px'
                            , icon: 1
                            , time: 1000
                        });
                    }
                }
            });
            return false; // 阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    })

    function back(){
        location.href = layui.setter.baseLocal + "sys/resource/index";
    }
</script>
